package com.mallsystem.userservice.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mallsystem.common.bean.User;
import com.mallsystem.common.exception.AllException;
import com.mallsystem.common.service.UserService;
import com.mallsystem.userservice.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;

@DubboService
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) throws AllException {
        //根据用户名查询判断用户是否存在
        User user = userMapper.selectByUsernameInt(username);
        if (user == null) {
            throw new AllException("用户名存在，请重新输入");
        }
        //判断用户状态是否禁用或注销
        if(user.getStatus().equals(2) || user.getStatus().equals(3)){
            throw new AllException("用户名已被禁用或注销");
        }
        //对输入密码进行加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(password + user.getSalt()));
        //和数据库中的密码对比
        if(!md5Pwd.equals(user.getPassword())){
            throw new AllException("密码错误，请重新输入");
        }
        return user;
    }

    @Override
    public boolean reg(User user) throws AllException {
        //判断该用户名是否已经存在
        if (userMapper.selectByUsernameInt(user.getUsername()) != null) {
            throw new AllException("用户名存在，无法注册");
        }
        //生成盐
        String salt = RandomUtil.randomString(8);
        user.setSalt(salt);
        //对密码进行加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(user.getPassword() + salt));
        user.setPassword(md5Pwd);
        //设置初始支付密码
        String payPwd = SecureUtil.md5(SecureUtil.md5("123" + salt));
        user.setPayPassword(payPwd);
        //添加用户
        return userMapper.insert(user) == 1;
    }

    @Override
    public boolean update(User user) throws AllException {
        User oldUser = userMapper.selectById(user.getId());
        if(oldUser == null){
            throw new AllException("用户不存在");
        }
        /*
        1.用户名不能重复
        2.身份证不能相同
         */
        User user1 = new User();
        user1.setUsername(user.getUsername());
        if (userMapper.selectByCondition(user1)
                .stream()
                .anyMatch(item -> item.getUsername().equals(user.getUsername()) && !item.getId().equals(user.getId()))) {
            throw new AllException("用户名重复，无法修改");
        }
        User user2 = new User();
        user2.setIdCard(user.getIdCard());
        if(user.getIdCard() != null){
            if (userMapper.selectByCondition(user2)
                    .stream()
                    .anyMatch(item -> item.getIdCard().equals(user.getIdCard()) && !item.getId().equals(user.getId()))) {
                throw new AllException("身份证号重复，无法修改");
            }
        }
        return userMapper.update(user) == 1;
    }

    @Override
    public PageInfo<User> selectByCondition(User condition, Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //查询
        List<User> user = userMapper.selectByCondition(condition);
        //创建分页信息
        PageInfo<User> pageInfo = new PageInfo<>(user);
        return pageInfo;
    }

    @Override
    public User selectById(Integer id)  {
        return userMapper.selectById(id);
    }

    @Override
    public boolean updateAllPassword(Integer id,String password,String payPassword) throws AllException {
        User oldUser = userMapper.selectById(id);
        if(oldUser == null){
            throw new AllException("用户不存在");
        }
        String salt = oldUser.getSalt();
        String newPassword = SecureUtil.md5(SecureUtil.md5(password + salt));
        String newPayPassword = SecureUtil.md5(SecureUtil.md5(payPassword + salt));
        String passwordInfo = password == null ? oldUser.getPassword() : newPassword;
        String payPasswordInfo = payPassword == null ? oldUser.getPayPassword() : newPayPassword;
        oldUser.setPassword(passwordInfo);
        oldUser.setPayPassword(payPasswordInfo);
        return userMapper.update(oldUser) == 1;
    }

    @Override
    public boolean updateStatus(User user) throws AllException {
        User oldUser = userMapper.selectById(user.getId());
        if(oldUser == null){
            throw new AllException("用户不存在");
        }
        Integer status = oldUser.getStatus() == 1 ? 2 : 1;
        oldUser.setStatus(status);
        return userMapper.update(oldUser) == 1;
    }

    @Override
    public boolean updatePassword(Integer id,String newPwd,String oldPwd) throws AllException {
        User user = userMapper.selectById(id);
        if(user == null){
            throw new AllException("用户不存在");
        }
        String old = SecureUtil.md5(SecureUtil.md5(oldPwd + user.getSalt()));
        if(!old.equals(user.getPassword())){
            throw new AllException("初始密码错误，请重新输入");
        }
        String password = SecureUtil.md5(SecureUtil.md5(newPwd + user.getSalt()));
        User newUser = new User();
        newUser.setId(id);
        newUser.setPassword(password);
        return userMapper.update(newUser) == 1;
    }

    @Override
    public boolean updatePayPassword(Integer id, String newPayPwd,String oldPayPwd) throws AllException {
        User user = userMapper.selectById(id);
        if(user == null){
            throw new AllException("用户不存在");
        }
        String old = SecureUtil.md5(SecureUtil.md5(oldPayPwd + user.getSalt()));
        if(!old.equals(user.getPayPassword())){
            throw new AllException("初始密码错误，请重新输入");
        }
        String newPassword = SecureUtil.md5(SecureUtil.md5(newPayPwd + user.getSalt()));
        User newUser = new User();
        newUser.setId(id);
        newUser.setPayPassword(newPassword);
        return userMapper.update(newUser) == 1;
    }

    @Override
    public boolean recharge(Integer id, BigDecimal sum) throws AllException {
        User user = userMapper.selectById(id);
        if(user == null){
            throw new AllException("用户不存在");
        }
        BigDecimal all = sum.add(user.getMoney());
        User newUser = new User();
        newUser.setId(id);
        newUser.setMoney(all);
        return userMapper.update(newUser) == 1;
    }
}
